/**
 * @file    app_nvm_mng.c
 * @brief   存储任务管理
 * @author  Liuwei
 * @version 1.0.1
 * @date    2023-04-27
 * 
 * @copyright Copyright (c) 2023 JBD-Energy Storage Technology Co. LTD
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version   <th>Author   <th>Description
 * <tr><td>2023-04-27 <td>1.0.1     <td>name     <td>首次创建
 * </table>
 */
/* includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "If_do.h"
#include "task.h"
#include "cfg_prj.h"
#include "If_nvm.h"
#include "event_mng.h"
#include "If_rtc.h"
#include "app_sample.h"
#include "app_monitor.h"
#include "param_mng.h"
/* macro/define --------------------------------------------------------------*/
#define APP_NVM_MNG_TASK_DELAY            (500)
/* typedef -------------------------------------------------------------------*/

/* local functions -----------------------------------------------------------*/
static uint8_t frist_power = 0;
/* static variables ----------------------------------------------------------*/

/* global variables ----------------------------------------------------------*/

/** ------------------------------------------------------------------------- *
  *                             Global function
 ** ------------------------------------------------------------------------- */
/**
 * @brief  存储管理任务
 * @param  pvParameters
 */
void vNvmMngTasks(void *pvParameters)
{
	uint16_t ret_cnt = 0;
//    portTickType xLastExecutionTime;

//    xLastExecutionTime = xTaskGetTickCount();

	task_run_reset(TASK_ID_NVM);
	
	ret_cnt = param_get_data(PAR_ID_RESTART_TIMES) + 1;
	param_set_data(PAR_ID_RESTART_TIMES , ret_cnt);
	
    for( ;; )
    {
		/** 上电记录事件 必须等待一次采样完成 **/
		if((frist_power == 0) && (TRUE == sample_complete()))
		{
			event_add_to_sram(EVT_ID_POWER_UP , 0 , 0);
			
			frist_power = 1;
		}
		
        event_write_to_nvm(); /* 更新缓冲区的历史记录到存储区域 */
		
        task_run_count(TASK_ID_NVM);
        
	    vTaskDelay(APP_NVM_MNG_TASK_DELAY);
    }
}

/** ------------------------------------------------------------------------- *
  *                              Local function
 ** ------------------------------------------------------------------------- */

/********** Copyright (c) 2023 JBD-Energy Storage Technology Co. LTD *********/


